package com.youdianstar.app.entity

import com.youdianstar.app.common.DataCode
import com.youdianstar.app.common.util.StrUtil


/**
 * File: RentValueEx
 * Author: Administrator Create: 2025/1/20 0020 15:26
 * Changes (from 2025/1/20 0020)
 * --------------------------------------------------
 * description: 租借数据
 * ---------------------------------------------------
 *
 */

/**
 * 判断是否能租借
 */
data class RentCheck(val rentOrNot :Int,val payMoney :Double,val orderNo:String?){
    //rentOrNot  0:可以租借 1：没有绑定银行卡 2：有未支付订单 3：子账号有未完成订单 4：租借订单已达上限
    //           5：绑定的卡还有即将过期 6:主账号有未支付订单 7:主账号绑定的卡过期或异常状态 8：主账号绑定的卡即将过期 9:调查中的订单已达到上限
}

/**
 * 扫码获取计费规则, 可能跟其他接口不一致,单独写
 */
data class ReqStationPlan(
    val branchName:String?,
    //0-机柜可用 1-充电宝电量过低 2-项目信息 3-其他租户信息
    val code: Int,
    val billing: BillingPlan?,
    //押金预付款(元)
    val depositAmount:Double = 0.0,
    //已付押金信息
    val paidDepositAmount:Double?=null,
    //预付款金额
    val prepaymentAmount:Double = 0.0,
    //预授权金额(元)
    val preAuthAmount:Double = 0.0,
    //机柜信息
    val maxBc: Int?
) {
    fun paidDepositValue():Double{
        return paidDepositAmount?:0.0
    }
}

//---------------------------------预授权租借------------------------

/**租借下单 - 预授权*/
data class RentalAuthRequest(
    //租借类型：1.Stripe绑定卡授权 2.Stripe预授权谷歌支付 3.Stripe预授权苹果支付
    val rentType: Int?,
    //Google payment token
    val paymentTokenObject: String? = null,
    //渠道参数
    val paymentChannel: String? = "stripe",
    //机柜编号: SN ; 机柜QR数据
    val scanUrl: String,
    //支付关键信息，客户id, rentType=1时必传
    val customerId: String? = null,
    //支付关键信息，卡token, rentType=2时必传
    val cardToken: String? = null,
    //支付关键信息，卡id, rentType=3时必传
    val paymentMethodId: String? = null,
    //1-App
    val source: Int,
    //银行卡id
    val bankcardId: Int? = null,
    //银行卡信息
    val bankcardInfo: CardBindReq? = null
)

/**租借下单 - Stripe授权*/
data class RentalAuthRes(
    //租借类型：1.预授权卡支付 2.预授权谷歌支付 3.预授权苹果支付
    val rentType:Int,
    //1.预授权租借 2.已授权租借 3.白名单租借 4.已交押金租借
    val scene:Int,

    //押金金额
    val depositAmount:Double,
    //预授权金额
    val preAuthAmount:Double,
    //预付款金额
    val prepaymentAmount:Double,

    val resp: RentalAuthResp?,

    val order:OrderInfo?,
    val amount:Double,
    val clientSecret:String,
    val failedReason:String,
    val success:Int,
)

data class RentalAuthResp(
    //支付状态：0-未支付 10-支付成功 11-已授权 20-已退款 30-支付关闭
    val status: Int,
    val outTradeNo: String,
    val channelUserId: String,
    val failedReason: String,
    val clientSecret: String,
    val displayMode: String,
    val displayContent: String,
    val channelOrderNo: String? = null,
)


//--------------------------------------------------------------

/**
 * 租借订单轮询
 */
data class RentResult(
    /**
     * 订单号
     */
    val orderNo: String,

    /**
     * 充电宝号
     */
    val pbNo: String,

    /**
     * 仓位号
     */
    val pos: Int
)

/**订单轮询*/
data class OrderQuery(
    val rent :List<OrderInfo>,
    val unpaid:List<OrderInfo>,
    val surveying:List<OrderInfo>
)

/*订单数据*/
data class OrderInfo(
    /* 0:Rent order 1:Swapping */
    var pageType : Int = 0,
    /**订单编号*/
    val orderNo: String,
    /**创建时间 毫秒*/
    val createTime: Long = 0,
    /**归还时间*/
    val endTime: Long = 0,
    /**当前时间*/
    val nowTime: Long? = null,
    /**使用时长-秒*/
    val useDuration: Long? = null,
    /**计费时长-秒*/
    val billDuration: Long? = null,

    //充电卡免费时长
    val chargeCardDuration: Long,
    /*租借网点地址*/
    val rentBranchName: String?=null,
    /*租借机柜编号*/
    val rentCabinetNo:String?=null,
    /*充电宝编号*/
    val pbNo:String?=null,
    /*归还网点地址-支持置换业务*/
    val returnBranchName: String?=null,
    /*归还机柜编号*/
    val returnCabinetNo:String?=null,
    /**计费规则*/
    val billing : BillingPlan?=null,

    /**
     * 状态为待支付的订单，需要支付的金额
     */
    val money: Double = 0.0,
    /**
     * 实际金额 --支付后记录金额
     */
    var realAmount: Double = 0.0,
    /**
     * 订单金额/订单租金
     */
    val orderAmount: Double? = null,

    /**税率*/
    val tax:Double = 0.0,
    /**税额金额*/
    val taxAmount: Double = 0.0,

    /**平台优惠金额*/
    val plateCouponAmount: Double? = null,
    /**优惠券金额*/
    val couponAmount: Double? = null,
    val userCouponId:Int=0,
    val userCouponInfo:CouponRes?=null,
    /**优惠码金额*/
    val promoCodeAmount:Double? = null,
    val userPromoCodeId:Int = 0,
    val userPromoCodeInfo:CouponRes?=null,
    /**优惠后的金额*/
    val discountAfterAmount: Double? = null,
    /**
     * 罚金(已支付的)
     */
    var penaltyAmount: Double? = null,
    /**
     * 退款金额 --租借订单
     */
    val refundAmount: Double? = null,
    /**
     * 是否免单
     * --支付计算金额时使用
     */
    val isFree:Boolean = false,

    /**
     * 订单状态：0-初始化 1-租借中 2-待支付 3-已完成 4-已取消 5-调查中 6-已退款 7-已报失
     * 置换状态: 0-待置换 1-已完成
     */
    val state: Int = 0,
    /**
     * 支付方式: 0-钱包支付 1-卡支付 2-亲情圈 3-Apple 4-google 5-paypal 6-weChat 7-aliPay
     */
    val paymentMethod: Int = -1,
    /**
     * 支付状态：0-未支付 1-已支付 2-免费 3-部分退款 4-全部退款
     */
    val paymentState: Int = 0,

    /**
     * 是否报失 1是 0 否
     */
    val lost: Boolean = false,

    /**处理状态 : 1-充电宝在 2-充电宝丢失*/
    val processState : Int? = null,
    /**上报时间*/
    val reportTime : Long? = null,
    /**处理状态 0-等待审核 1-审核完成*/
    val reportState:Int? = null
) {
    /**租借当前时间*/
    fun getRentingNow():Long{
        if (null == nowTime || nowTime==0L)
            return System.currentTimeMillis()
        return nowTime
    }
    //使用时长秒
    fun getOrderDuration():Long{
        if (state == DataCode.STATE_RENTING && createTime>0L){
            return (getRentingNow() - createTime)/1000
        }
        return useDuration?:0
    }

    fun orderAmountValue():Double{
        return orderAmount?:0.0
    }

    fun platAmountValue():Double{
        return plateCouponAmount?:0.0
    }

    fun couponAmountValue():Double{
        return couponAmount?:0.0
    }

    fun promoAmountValue():Double{
        return promoCodeAmount?:0.0
    }

    /*待支付金额*/
    fun unpaidAmountValue():Double{
        return money
    }

    /*实际支付金额*/
    fun realAmountValue():Double{
        return realAmount
    }

    /*退款金额*/
    fun refundAmountValue():Double{
        return refundAmount?:0.0
    }


    fun getRentAddress():String{
        return if (StrUtil.isEmpty(rentBranchName))"--" else rentBranchName!!
    }

    fun getReturnAddress():String{
        return if (StrUtil.isEmpty(returnBranchName))"--" else returnBranchName!!
    }

    fun pbNoTx():String{
        return pbNo?:"--"
    }
}

